<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <style>
        .nav {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }

        .container {
            padding-top: 80px;
            height: auto;
        }

        .container-right {
            width: auto;
        }

        .blog-pagnation-wrapper {
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }

        .blog-pagnation-item {
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }

        .blog-pagnation-item:hover {
            background: #4e4eeb;
            color: #fff;
        }

        .blog-pagnation-item.actvie {
            background: #4e4eeb;
            color: #fff;
        }
    </style>
    <script src="js/jquery.min.js"></script>
    <script src="js/common.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/log.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="login.html">登陆</a>
        <a href="blog_list.html">主页</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div class="container-right" style="width: 100%;">
            <div id="artList">
                <!-- 每一篇博客包含标题, 摘要, 时间 -->
                <!-- <div class="blog" >
                <div class="title">我的第一篇博客</div>
                <div class="date">2021-06-02</div>
                <div class="desc">正文</div>
                <a href="blog_content.html?id=1" class="detail">查看全文 &gt;&gt;</a>
                 </div> -->
            </div>


            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="goFirstPage()">首页</button>
                <button class="blog-pagnation-item" onclick="goBeforePage()">上一页</button>
                <button class="blog-pagnation-item" onclick="goNextPage()">下一页</button>
                <button class="blog-pagnation-item" onclick="goLastPage()">末页</button>
            </div>
        </div>
    </div>
    <script>
        // 当前页码
        var pindex = 1;
        // 每页显示条数
        var psize = 3;
        //最大页数
        var pcount=1;
        //1.尝试获取当前url中的页码
        pindex = (getUrlValue("pindex") == "" ? 1 : getUrlValue("pindex"));
        //2.查询后端接口得到当前页面的数据，进行显示
        function initPage() {
            jQuery.ajax({
                url: "/art/listbypage",
                type: "get",
                data: { "pindex": pindex, "psize": psize },
                success: function (result) {
                    if (result != null && result.code == 200 && result.data.list.length > 0) {
                        //存在文章
                        var artListDiv = "";
                        for (var i = 0; i < result.data.list.length; i++) {
                            var artItem = result.data.list[i];
                            artListDiv += '<div class="blog">';
                            artListDiv += '<div class="title">' + artItem.title + '</div>';
                            artListDiv += '<div class="date">' + artItem.updatetime + '</div>';
                            artListDiv += '<div class="desc">' + artItem.content + '</div>';
                            artListDiv += '<a href="blog_detail.html?id=' + artItem.id + '">查看全文</a>';
                            artListDiv += ' </div>';
                        }
                        jQuery("#artList").html(artListDiv);
                        pcount=result.data.pcount;
                    }else if(result!=null&&result.code==-1){
                        location.href="blog_lists.html?pindex="+result.data.pcount;
                    }else {
                        //没有发表过文章
                        jQuery("#artList").html("<h3>暂无文章</h3>");
                    }
                }
            });
        }
        initPage();

        function goFirstPage(){
            location.href="blog_lists.html";
        }

        function goBeforePage(){
            if(pindex<=1){
                alert("当前已经在首页");
                return;
            }
            pindex=parseInt(pindex)-1;
            location.href="blog_lists.html?pindex="+pindex;
        }
        function goNextPage(){
            if(pindex>=pcount){
                alert("已经是最后一页");
                return;
            }
            pindex=parseInt(pindex)+1;
            location.href="blog_lists.html?pindex="+pindex;
        }
        function goLastPage(){
            if(pindex>pcount){
                alert("已经是最后一页");
                return;
            }
            location.href="blog_lists.html?pindex="+pcount;
        }


    </script>
</body>

</html>